//  Inside Microsoft Exchange 2007 Web Services 
//  Chapter 21
//
//  Copyright (c) 2007 David Sterling, Ben Spain, Mark Taylor, Huw Upshall, Michael Mainer.  
//  All rights reserved.
//
using System;
using System.Collections.Generic;
using System.Text;
using ProxyHelpers.EWS;

namespace PrintFreeBusyInformation
{
    class Program
    {
        static void Main(string[] args)
        {
            // Need to retrieve a valid FreeBusyResponseType from GetUserAvailability before calling
            // PrintFreeBusyInformation
        }
        /// <summary>
        /// Takes a FreeBusyResponseType instance along with a friendly name to 
        /// associate with it, and prints to the Console the free/busy information. Listing 21-38
        /// </summary>
        /// <param name="fbResponse">Instance of a FreeBusy response</param>
        /// <param name="participantName">Name to associate with the FreeBusy 
        /// information</param>
        public static void PrintFreeBusyInformation(
            FreeBusyResponseType freeBusyResponse,
            string attendeeName)
        {
            // Extract the FreeBusyView from the response
            //
            FreeBusyView freeBusyView = freeBusyResponse.FreeBusyView;

            // Determine what type of information the freeBusyView contains based 
            // on the FreeBusyViewType value
            //
            bool containsMergedData =
                (freeBusyView.FreeBusyViewType == FreeBusyViewType.MergedOnly ||
                freeBusyView.FreeBusyViewType == FreeBusyViewType.DetailedMerged ||
                freeBusyView.FreeBusyViewType == FreeBusyViewType.FreeBusyMerged);

            bool containsCalendarEvents =
                (freeBusyView.FreeBusyViewType != FreeBusyViewType.MergedOnly &&
                freeBusyView.FreeBusyViewType != FreeBusyViewType.None);

            bool containsCalendarEventDetails =
                (freeBusyView.FreeBusyViewType == FreeBusyViewType.Detailed ||
                freeBusyView.FreeBusyViewType == FreeBusyViewType.DetailedMerged);

            Console.WriteLine("FreeBusy view for [ " + attendeeName + " ]:");
            Console.WriteLine("\tHas Merged Data: " + containsMergedData);
            Console.WriteLine("\tHas Calendar Events: " + containsCalendarEvents);
            Console.WriteLine("\tHas Calendar Event Details: " +
                containsCalendarEventDetails);

            // Display Merged Free Busy data if we have it.
            if (containsMergedData)
            {
                Console.WriteLine("Merged Free Busy: " +
                    freeBusyView.MergedFreeBusy);
            }

            // Display Calendar Events if they are present, (and 
            // CalendarEventDetails as well.)
            //
            //  Why the check for freeBusyView.CalendarEventArray == null?  Because,
            //  even though the view type is FreeBusy or Detailed, this user
            //  may have no events on this day, in which case the event array 
            //  will be null.
            //
            if (containsCalendarEvents && freeBusyView.CalendarEventArray != null)
            {
                Console.WriteLine("Listing of Calendar Events:");

                foreach (CalendarEvent calendarEvent in freeBusyView.CalendarEventArray)
                {
                    Console.WriteLine(String.Format("Event ({0}) {1} - {2}",
                        calendarEvent.BusyType,
                        calendarEvent.StartTime,
                        calendarEvent.EndTime));

                    if (containsCalendarEventDetails)
                    {
                        // Do not attempt to print Subject and Location if the event is 
                        // private.  The properties will have null values in the 
                        // CalendarEventDetails anyway.
                        //
                        if (calendarEvent.CalendarEventDetails.IsPrivate)
                        {
                            Console.WriteLine("\tDetails: <Private Details Omitted>");
                        }
                        else
                        {
                            Console.WriteLine("\tDetails:");
                            Console.WriteLine("\t\tSubject: " +
                                calendarEvent.CalendarEventDetails.Subject);
                            Console.WriteLine("\t\tLocation: " +
                                calendarEvent.CalendarEventDetails.Location);
                        }

                        // The rest of the details, always present reguarless of 
                        // IsPrivate flag
                        //
                        Console.WriteLine("\t\tMeeting: {0}, Recurring {1}\r\n" +
                            "\t\tException {2}, ReminderSet {3}",
                            calendarEvent.CalendarEventDetails.IsMeeting,
                            calendarEvent.CalendarEventDetails.IsRecurring,
                            calendarEvent.CalendarEventDetails.IsException,
                            calendarEvent.CalendarEventDetails.IsReminderSet);
                    }
                }
            }
            // Final trailing line for nice formatting
            Console.WriteLine();
        }

    }
}
